<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\model;

class WaterEleRecord extends BaseModel
{
    // protected $type = [
    //     'record_date'   => 'timestamp',
    // ];
    public function setRecordDateAttr($value){
        return strtotime($value);
    }

    // public function getRecordDateAttr($value){
    //     // halt($value);
    //     return date('Y-m-d',$value);
    // }
    /**
     * @Description: 获取上次水电表读数
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $params
     * @Date: 2022-11-22 11:25:57
     * @version: 1.0.0
     * @return [type]
     */    
    public static function getWaterEle($where){
        
        // $where['state'] = $params['state'];
        
        $res = self::where($where)->order('record_date','desc')->find();

        return $res;
    }

    public function room(){
        return $this->belongsTo('Room','room_id')->bind([
            'room_name' => 'name'
        ])->with('building');
    }

    /**
     * @Description: 获取水电记录
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-22 17:42:50
     * @version: 1.0.0
     * @return [type]
     */
    public static function getWaterEleList(){

        $res = self::limit(20)->order('id desc')->with('room')->select();

        return $res;
    }
    /**
     * @Description: 关联维护员
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-23 16:56:17
     * @version: 1.0.0
     * @return [type]
     */
    public function repairUser(){
        return $this->belongsTo('repairUser', 'user_id')->bind([
            'real_name' => 'real_name'
        ]);
    }
    /**
     * @Description: 关联管理员
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-23 16:57:21
     * @version: 1.0.0
     * @return [type]
     */
    public function adminUser(){
        return $this->belongsTo('Admin', 'user_id')->bind([
            'nickname' => 'nickname'
        ]);
    }
    /**
     * @Description: 关联楼房
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-23 17:02:51
     * @version: 1.0.0
     * @return [type]
     */
    public function building(){
        return $this->hasOneThrough('building','room','id', 'id', 'room_id', 'building_number');
    }
}
